// skin_static.c
#include "skin.h"

int 		skin_static_onpaint(void* self)
{
	PSKIN_STATIC p = (PSKIN_STATIC) self;
	PSKIN_CLIENT control = (PSKIN_CLIENT) p;
	PSKIN_WINDOW pwnd = 0;
#if (SKIN_DEBUG == 1)
	printf("Static.OnPaint()\r\n");
#endif`
	if (p)
	{
		if (control->client_surface)
		{
#if (SDL_TTF_USE == 1)
			pwnd = (PSKIN_WINDOW) control->parent;
			if (pwnd && pwnd->default_font)
			{
				SDL_Rect rect;
				rect.x = 0;
				rect.y = 0;
				rect.w = control->rect.cx - control->rect.x;
				rect.h = control->rect.cy - control->rect.y;
				if (!p->text_surface)
				{
					p->text_surface = TTF_RenderUTF8_Blended(pwnd->default_font, "한글", p->foreground_color); //, p->background_color);
				}
				if (p->text_surface)
				{
					SDL_BlitSurface(p->text_surface, NULL, control->client_surface, &rect);
				}				
				return 1;
			}
#endif 
		}
	}
	return 0;
}

int skin_static_set_foregroundcolor(PSKIN_STATIC p, int red, int green, int blue)
{
	if (p)
	{
		p->foreground_color.r = red;
		p->foreground_color.b = blue;
		p->foreground_color.g = green;
		return 1;
	}
  return 0;
}

int skin_static_set_backgroundcolor(PSKIN_STATIC p, int red, int green, int blue)
{
	if (p)
	{
		p->background_color.r = red;
		p->background_color.b = blue;
		p->background_color.g = green;
		return 1;
	}
  return 0;
}

int 		skin_static_set_caption(PSKIN_STATIC p, const char* caption)
{
	if (p)
	{
		SDL_Rect rect;
		PSKIN_WINDOW pwnd = 0;
		PSKIN_CLIENT control = (PSKIN_CLIENT) p;
		pwnd = (PSKIN_WINDOW) control->parent;

		rect.x = 0;
		rect.y = 0;
		rect.w = control->rect.cx - control->rect.x;
		rect.h = control->rect.cy - control->rect.y;
		if (p->text_surface)
		{
			SDL_FreeSurface(p->text_surface);
			p->text_surface = 0;
		}
		if (!p->text_surface)
		{
			p->text_surface = TTF_RenderUTF8_Blended(pwnd->default_font, caption, p->foreground_color); //, p->background_color);
		}
		/*if (control->client_surface)
		{
			SDL_FreeSurface(control->client_surface);
			control->client_surface = 0;
		}
		control->client_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, rmask, gmask, bmask, amask);*/
		if (p->text_surface)
		{
			// clean client surface
			SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 128, 128, 128));
			SDL_BlitSurface(p->text_surface, NULL, control->client_surface, &rect);
			skin_windowupdate((PSKIN_WINDOW) control->parent);
		}
		return 1;
	}
	return 0;
}